﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Configuration;

namespace PeerComLink
{
    public class RunningData
    {

        public Member LocalMember { get; set; }

        public MTObservableCollection<MemberStatus> MemberStatuses
        {
            get
            {
                return _memberStatuses;
            }
        }

        MTObservableCollection<MemberStatus> _memberStatuses;

        ComChannel _cchannel;

        public RunningData()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var appSettings = config.GetSection("appSettings") as AppSettingsSection;
            var settings = appSettings.Settings;

            string userName = settings["UserName"].Value;
            string guid = settings["Guid"].Value;
            if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(guid))
            {
                LocalMember = Member.Create();
                settings["Guid"].Value = LocalMember.ID.ToString();
                config.Save(ConfigurationSaveMode.Modified);
            }
            else
            {
                LocalMember = new Member
                {
                    ID = new Guid(guid),
                    DisplayName = userName
                };
            }

            LocalMember.PropertyChanged +=
            (object sender, System.ComponentModel.PropertyChangedEventArgs e) =>
            {
                _cchannel.SendIntroductionBroadcast(LocalMember);
                settings["UserName"].Value = LocalMember.DisplayName;
                config.Save(ConfigurationSaveMode.Modified);
            };
            _cchannel = new ComChannel();
            _cchannel.SendIntroductionBroadcast(LocalMember);

            _memberStatuses = new MTObservableCollection<MemberStatus>();
        }

        public void InitiateCall(MemberStatus memberStatus)
        {
            _cchannel.InitiateCall(memberStatus);
        }
        public void AcceptCall(MemberStatus memberStatus)
        {
            _cchannel.AcceptCall(memberStatus);
        }
        public void EndCall(MemberStatus memberStatus)
        {
            _cchannel.EndCall(memberStatus);
        }
    }
}
